home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / CurrencyTextField.java < prev    next >
Text File  |  1998-09-28  |  9KB  |  252 lines

  1. package com.symantec.itools.awt;
  2.  
  3. import java.awt.event.KeyEvent;
  4. import java.awt.event.KeyAdapter;
  5. import java.awt.event.FocusEvent;
  6. import java.awt.event.FocusAdapter;
  7. import java.awt.datatransfer.*;
  8. import com.symantec.itools.swing.CurrencyEngine;
  9.  
  10. public class CurrencyTextField extends java.awt.TextField {
  11.     public CurrencyTextField(                   ) { this(""         , 0              );    }
  12.     public CurrencyTextField(int numberOfColumns) { this(""         , numberOfColumns); }
  13.     public CurrencyTextField(String  initialText) { this(initialText, 256            ); }
  14.  
  15.   // All other constructors call this one.
  16.     public CurrencyTextField(String initialText, int numberOfColumns)    {
  17.         super(initialText, numberOfColumns);
  18.     }
  19.  
  20.   public synchronized void setFormattedText(String data) {
  21.       StringBuffer s = new StringBuffer();
  22.       _dataText = data;
  23.       int pos = _engine.initDisplay(data, s);
  24.       if (!_haveFocus) {
  25.         StringBuffer newData = new StringBuffer();
  26.         _engine.postFormat(s.toString(), newData);
  27.         s = newData;
  28.         _isFormatted = true;
  29.         if (_keyListener == null) {
  30.           setText(s.toString());
  31.           return;  // don't call peer if it doesn't exist yet
  32.         }
  33.       }
  34.       rightJustify(s);
  35.       setText(s.toString());
  36.       setCaretPosition(s.length());
  37.       if (isEditable() && _haveFocus) {
  38.         setCaretPosition(pos + _padCount);
  39.         if (data.length() == 0 && !_engine.getATMmode())
  40.           select(_padCount, s.length());
  41.         _isFormatted = false;
  42.       }
  43.   }
  44.  
  45.   public synchronized void format() {
  46.       StringBuffer newData = new StringBuffer();
  47.       _engine.postFormat(getDataText(), newData);
  48.       rightJustify(newData);
  49.       setText(newData.toString());
  50.       setCaretPosition(newData.length());
  51.       _isFormatted = true;
  52.   }
  53.  
  54.   public synchronized String getDataText() {
  55.       if (_dataText == null)
  56.         _dataText = getText().trim();
  57.       return _dataText;
  58.   }
  59.  
  60.   //  Get/Set properties: all are properties of the currency engine
  61.   public boolean isCurrencyLeading    (         ) { return _engine.getCurrencyLeading   ( ); }
  62.   public boolean isSeparatorEnabled   (         ) { return _engine.getCommas            ( ); }
  63.   public boolean getATMmode           (         ) { return _engine.getATMmode           ( ); }
  64.   public String  getCurrencySymbol    (         ) { return _engine.getCurrencySymbol    ( ); }
  65.   public char    getDecimalPoint      (         ) { return _engine.getDecimalPoint      ( ); }
  66.   public char    getSeparator         (         ) { return _engine.getSeparator         ( ); }
  67.   public int     getDigitsAfterDecimal(         ) { return _engine.getDigitsAfterDecimal( ); }
  68.   public void    setCurrencyLeading   (boolean b) {        _engine.setCurrencyLeading   (b); }
  69.   public void    setSeparatorEnabled  (boolean b) {        _engine.setCommas            (b); }
  70.   public void    setATMmode           (boolean b) {        _engine.setATMmode           (b); }
  71.   public void    setCurrencySymbol    (String  s) {        _engine.setCurrencySymbol    (s); }
  72.   public void    setDecimalPoint      (char    c) {        _engine.setDecimalPoint      (c); }
  73.   public void    setSeparator         (char    c) {        _engine.setSeparator         (c); }
  74.   public void    setDigitsAfterDecimal(int     n) {        _engine.setDigitsAfterDecimal(n); }
  75.  
  76.   public synchronized void cut(Clipboard clipboard) {
  77.       if (!isEditable())
  78.         return;
  79.       String s = getText().trim();
  80.       int selStart = getSelectionStart() - _padCount;
  81.       int selEnd = getSelectionEnd() - _padCount;
  82.       StringSelection ss = new StringSelection(s.substring(selStart, selEnd));
  83.       clipboard.setContents(ss, ss);
  84.       StringBuffer newData = new StringBuffer(s);
  85.       _engine.cut(newData, selStart, selEnd);
  86.       rightJustify(newData);
  87.       setText(newData.toString());
  88.       select(0,0);
  89.       setCaretPosition(getText().length());
  90.       setCaretPosition(selStart + _padCount);
  91.   }
  92.  
  93.   public synchronized void paste(Clipboard clipboard) {
  94.       if (!isEditable())
  95.         return;
  96.       String pasteData = "";
  97.       try {
  98.           pasteData = (String)clipboard.getContents(this).getTransferData(DataFlavor.stringFlavor);
  99.       } catch (Exception e) {}
  100.       int selStart = getSelectionStart() - _padCount;
  101.       int selEnd   = getSelectionEnd()   - _padCount;
  102.       int pos      = getCaretPosition()  - _padCount;
  103.       StringBuffer data = new StringBuffer(getText().trim());
  104.       _engine.paste(data, pasteData, pos, selStart, selEnd);
  105.       rightJustify(data);
  106.       setText(data.toString());
  107.       select(0,0);
  108.       setCaretPosition(getText().length());
  109.       setCaretPosition(pos + _padCount);
  110.   }
  111.  
  112.   /**
  113.    * This gets called when this component is added to a container.
  114.    * Typically, it is used to create this component's peer.
  115.    * It has been overridden to call addKeyListener.
  116.    *
  117.    * @see #removeNotify
  118.    */
  119.   public synchronized void addNotify() {
  120.     if (_focusListener == null) {
  121.       _focusListener = new FocusAdapter() {
  122.         public void focusGained(FocusEvent e) { gotFocus(e);  }
  123.         public void focusLost  (FocusEvent e) { lostFocus(e); }
  124.       };
  125.       addFocusListener(_focusListener);
  126.       if (isEditable()) {
  127.               _keyListener = new KeyAdapter() {
  128.           public void keyPressed (KeyEvent e) { keyPress  (e); }
  129.           public void keyTyped   (KeyEvent e) { keyType   (e); }
  130.           public void keyReleased(KeyEvent e) { keyRelease(e); }
  131.         };
  132.               addKeyListener(_keyListener);
  133.             }
  134.         }
  135.     super.addNotify();
  136.   }
  137.  
  138.   /**
  139.    * This gets called when this component is removed from a
  140.    * container. Typically, it is used to destroy the peers of this
  141.    * component and all its subcomponents.
  142.    * It has been overridden here to call removeKeyListener.
  143.    *
  144.    * @see #addNotify
  145.    */
  146.   public synchronized void removeNotify() {
  147.     if (_focusListener != null) {
  148.       if (isEditable())
  149.         removeKeyListener(_keyListener);
  150.         removeFocusListener(_focusListener);
  151.       _keyListener = null;
  152.         _focusListener = null;
  153.     }
  154.     super.removeNotify();
  155.   }
  156.  
  157.   void gotFocus(FocusEvent e) {
  158.     _haveFocus = true;
  159.     if (_isFormatted || getText().length() == 0)
  160.       setFormattedText(getDataText());
  161.     _dataText = null;
  162.   }
  163.  
  164.   void lostFocus(FocusEvent e) {
  165.     format();
  166.     _haveFocus = false;
  167.   }
  168.  
  169.   void keyPress(KeyEvent e) {
  170.     int selStart = getSelectionStart() - _padCount;
  171.     int selEnd   = getSelectionEnd()   - _padCount;
  172.     int pos      = getCaretPosition()  - _padCount;
  173.     if (pos < 0) {
  174.       setCaretPosition(_padCount);
  175.       e.consume();
  176.       if (selEnd > selStart)
  177.         select(_padCount, _padCount + Math.max(selEnd, 0));
  178.       return;
  179.     } else if (pos == 0) {
  180.       switch (e.getKeyCode()) {
  181.       case e.VK_LEFT:
  182.         if (selStart == pos && selStart < selEnd)
  183.           return;  // leading substring is selected, so let left work
  184.       case e.VK_HOME:
  185.         e.consume();
  186.       }
  187.     }
  188.     if (_engine.isHandledKey(e)) {
  189.       e.consume();  // prevent echo of keys we'll handle
  190.       if (_keyPressed)
  191.         processKey(e);
  192.       else
  193.         _keyPressed = true;
  194.     }
  195.   }
  196.  
  197.   protected void keyType(KeyEvent e) {
  198.   }
  199.  
  200.   protected void keyRelease(KeyEvent e) {
  201.     if (e.getKeyCode() == e.VK_HOME) {
  202.       setCaretPosition(getText().length());
  203.       setCaretPosition(_padCount);
  204.       _keyPressed = false;
  205.       return;
  206.     }
  207.     if (!_engine.isHandledKey(e))
  208.       return;
  209.     _keyPressed = false;
  210.     processKey(e);
  211.   }
  212.  
  213.   void processKey(KeyEvent e) {
  214.     int selStart = getSelectionStart() - _padCount;
  215.     int selEnd = getSelectionEnd() - _padCount;
  216.     int pos = getCaretPosition() - _padCount;
  217.     if (selStart < 0)  // selection starts in ozone?
  218.       selStart = 0;
  219.     if (selEnd < 0)    // entire selection was in ozone?
  220.       selEnd = 0;
  221.     if (pos < 0) { // allow no operations in the ozone
  222.       if (selEnd <= selStart)
  223.         return;
  224.       pos = selStart;  // selected text started in ozone but includes data
  225.     }
  226.     StringBuffer newData = new StringBuffer("");
  227.     StringBuffer data = new StringBuffer(getText().trim());
  228.     int newpos = _engine.processKey(e, pos, data.toString(), newData, selStart, selEnd);
  229.     if (newpos >= 0) {            // if good new caret position
  230.       rightJustify(newData);
  231.       setText(newData.toString());
  232.       select(0, 0);   // remove text selection, if any
  233.       setCaretPosition(newData.length());
  234.       setCaretPosition(newpos + _padCount);
  235.     } else if (newpos == -1)
  236.       setCaretPosition(pos + _padCount);
  237.   }
  238.  
  239.   void rightJustify(StringBuffer s) {
  240.     for (int i = 1; i <= _padCount; i++)
  241.       s.insert(0, ' ');
  242.   }
  243.  
  244.     private CurrencyEngine  _engine        = new CurrencyEngine();
  245.     private KeyAdapter      _keyListener   = null;
  246.      private FocusAdapter    _focusListener = null;
  247.   private final int       _padCount      = 128;  // padding spaces in currency field
  248.   private String          _dataText      = null;
  249.   private boolean         _haveFocus     = false;
  250.   private boolean         _isFormatted   = false;
  251.     private boolean         _keyPressed    = false;
  252. }